package com.hq.ims.data.service;

import com.hq.ims.data.config.verification.VdAdd;
import com.hq.ims.data.dao.CompanyAptitudeHistoryMapper;
import com.hq.ims.data.entity.CompanyAptitude;
import com.hq.ims.data.entity.CompanyAptitudeHistory;
import com.hq.ims.data.entityenum.EnumDataType;
import com.hq.ims.data.utils.LocalConstant;
import com.hq.ims.data.vo.AppFileMapstruct;
import com.hq.ims.data.vo.CompanyAptitudeMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.utils.util.ListUtils;
import com.hq.web.utils.SpringBootValidateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Author jiang bai ping
 * @Email 526666031@qq.com
 * @DateTime 2024-10-28 09:05:29
 * @Detail 企业资质服务实现类
 */
@Service
public class BaseCompanyAptitudeHistoryService extends BaseService<CompanyAptitudeHistoryMapper, CompanyAptitudeHistory> {

    @Resource
    private BaseFileRecordService baseFileRecordService;

    public List<CompanyAptitudeHistory> getCompanyAllAptitudeList(String id, Integer enumDataType) {
        return this.lambdaQuery().eq(CompanyAptitudeHistory::getCompanyCheckId, id)
                .eq(CompanyAptitudeHistory::getDateType, enumDataType)
                .list();
    }

    @Transactional(rollbackFor = Exception.class)
    public CompanyAptitudeHistory addData(CompanyAptitudeMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdAdd.class);

        //账值新的对象
        CompanyAptitudeHistory entity = new CompanyAptitudeHistory();
        entity.setCompanyCheckId(vo.getCheckId());
        entity.setCompanyId(vo.getCompanyId());
        entity.setCompanyAptitudeName(vo.getCompanyAptitudeName());
        entity.setLv(vo.getLv());
        entity.setValidEndDate(vo.getValidEndDate());
        entity.setDateType(EnumDataType.After);
        entity.insert();

        //保存资质文件
        AppFileMapstruct.FileVo fileVo = vo.getFileVo();
        if (fileVo != null) {
            baseFileRecordService.saveFile(fileVo, CompanyAptitude.class, entity.getCompanyAptitudeId(), LocalConstant.FILE_COMPANY_APTITUDE, true);
        }
        return entity;
    }

    @Transactional(rollbackFor = Exception.class)
    public void delByCompanyId(String companyId) {
        List<CompanyAptitudeHistory> dataList = this.lambdaQuery().select(CompanyAptitudeHistory::getCompanyAptitudeId).eq(CompanyAptitudeHistory::getCompanyId, companyId).list();
        if (ListUtils.isExistsEle(dataList)) {
            List<String> dataIdList = dataList.stream().map(CompanyAptitudeHistory::getCompanyAptitudeId).distinct().collect(Collectors.toList());
            for (String dataId : dataIdList) {
                baseFileRecordService.delFile(CompanyAptitudeHistory.class, dataId);
            }
            this.lambdaUpdate().eq(CompanyAptitudeHistory::getCompanyId, companyId).remove();
        }
    }
}
